﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Data.Common;
using System.Data.OleDb;

namespace HotelManagement.DL.Helper
{
    public static class ConnectionManager
    {
        private static DbConnection m_conDbConnection;
        private static DbProviderFactory m_facDbProviderFactory;
        private static string m_strConnectString = string.Empty;

        private static string DB_PATH = @"HotalMngDB.mdb";
        private static string DB_PROVIDER = "Microsoft.Jet.OLEDB.4.0";

        /// <summary>
        /// CreateConnString
        /// </summary>
        /// <returns></returns>
        public static string CreateConnString() 
        {
            string strConnString = string.Empty;
            
            OleDbConnectionStringBuilder connectStringBuilder = new OleDbConnectionStringBuilder();
            connectStringBuilder.DataSource = DB_PATH;
            connectStringBuilder.Provider = DB_PROVIDER;
            strConnString = connectStringBuilder.ConnectionString;
            return strConnString;
        }

        /// <summary>
        /// CreateConnection
        /// </summary>
        /// <returns></returns>
        public static DbConnection CreateConnection()
        {
            GetDbProviderFactory();
            m_conDbConnection = m_facDbProviderFactory.CreateConnection();
            if (string.IsNullOrEmpty(m_strConnectString)) 
            {
                m_strConnectString = CreateConnString(); 
            }
            m_conDbConnection.ConnectionString = m_strConnectString;

            return m_conDbConnection;
        }

        private static void GetDbProviderFactory()
        {
            m_facDbProviderFactory = OleDbFactory.Instance;
        }
    }
}
